package com.hisense.hiioc.hismartplate.server;

import com.alibaba.fastjson.JSONObject;
import com.hisense.hiioc.hismartplate.service.CoreManagerService;

/**
 * @Author: almo
 * @Date: 2019/3/19 上午11:58
 * @Version 1.0
 */
public class CmdDispatcher {

    private static volatile CmdDispatcher instance;

    private ICmdDispatcherListener listener;

    public static CmdDispatcher getInstance() {
        if (instance == null) {
            synchronized (CmdDispatcher.class) {
                if (instance == null)
                    instance = new CmdDispatcher();
            }
        }
        return instance;
    }

    public void initialize(ICmdDispatcherListener listener) {
        this.listener = listener;
    }

    public void dispatch(String text) {
        JSONObject jsonObject = JSONObject.parseObject(text);
        final String command = jsonObject.getString("protoCmd");
        if (command.equalsIgnoreCase(CoreManagerService.SOFTWARE_VERSION_CHECK)) {
            if (this.listener != null) {
                this.listener.onCheckVersionCmd(jsonObject.getJSONObject("extension").toString());
            }
        }
    }

    public interface ICmdDispatcherListener {
        void onCheckVersionCmd(String entity);
    }

}
